# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function

from ..ops import Mean
from ..base import OpsFeature

from .close import Close

class BIAS(OpsFeature):
    """Technical Indicator - BIAS

    - BIAS = (CLOSE - MA) / MA * 100
    """
    def __init__(self, feature=None, N=12):
        self.feature = feature or Close()
        self.N = N

    def __str__(self):
        name = type(self).__name__.lower()
        if str(self.feature) != 'close':
            name += '_'+str(self.feature)
        if self.N != 12:
            name += '_'+str(self.N)
        return name
    
    def _load_internal(self, instrument, start_time, end_time, freq):
        ma = Mean(self.feature, self.N)
        bias = (self.feature - ma) / ma * 100
        return bias.load(instrument, start_time, end_time, freq)
